package rwlivbk.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import rwlivbk.common.CommonResult;
import rwlivbk.mapper.IngredientMapper;
import rwlivbk.model.Dish;
import rwlivbk.model.Ingredient;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin-api/ingredients")
public class IngredientController {

    private final IngredientMapper ingredientMapper;

    @Autowired
    public IngredientController(IngredientMapper ingredientMapper) {
        this.ingredientMapper = ingredientMapper;
    }

    @GetMapping("/{dish_id}/ingredients")
    public CommonResult<?> getIngredientsByDishId(@PathVariable("dish_id") Integer dish_id) {
        List<Ingredient> ingredients = ingredientMapper.findIngredientsByDishId(dish_id);

        return CommonResult.success(ingredients);
    }

    @GetMapping("/getAllIngredients")
    public CommonResult<?> getAllIngredients() {

        ArrayList<Ingredient> ingredients = ingredientMapper.findAllIngredients();
        return CommonResult.success(ingredients);
    }

    @GetMapping("/getAllDeletedIngredients")
    public CommonResult<?> getAllDeletedIngredients() {

        ArrayList<Ingredient> ingredients = ingredientMapper.findAllDeletedIngredients();
        return CommonResult.success(ingredients);
    }

    @PostMapping("/addIngredient")
    public CommonResult<?> addDish(@RequestBody Ingredient ingredient) {
        ingredientMapper.insertIngredient(ingredient);
        return CommonResult.success(200);
    }

    @PutMapping("/updateIngredient")
    public CommonResult<?> updateDish(@RequestBody Ingredient ingredient) {
        ingredientMapper.updateIngredient(ingredient);
        return CommonResult.success(200);
    }

    @PostMapping("/addDishIngredient")
    public CommonResult<?> addDishIngredient(@RequestBody Map<String, Integer> data) {
        Integer dish_id = data.get("dish_id");
        Integer ingredient_id = data.get("ingredient_id");
        ingredientMapper.insertDishIngredient(dish_id, ingredient_id);
        return CommonResult.success(200);
    }


    @DeleteMapping("/delete/{dish_id}/{ingredient_id}")
    public CommonResult<?> deleteDish(@PathVariable Integer dish_id, @PathVariable Integer ingredient_id) {
        ingredientMapper.deleteDishIngredient(dish_id, ingredient_id);
        return CommonResult.success(200);
    }

}
